package com.amos.cmp_android.ui;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.amos.cmp_android.R;
import com.amos.cmp_android.model.SchoolCarModel;
import com.amos.cmp_android.utils.GsonUtil;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tlh on 2016/4/14.
 */
public class SearchSchoolBusActivity extends AppCompatActivity {
    private List<SchoolCarModel> totalList;
    private List<String> districtList;
    private List<List<String>> startPointList;
    private Spinner spinnerDistrict;
    private Spinner spinnerStartPoint;
    private RecyclerView recyclerView;
    private List<String> startPointStringList = new ArrayList<>();
    private ArrayAdapter<String> adapter_district;
    private ArrayAdapter<String> adapter_startPoint;
    private Button btnSearch;
    private String selectedDistrict;
    private String selectedStartPoint;
    private List<SchoolCarModel> displayList = new ArrayList<>();
    private BaseRecyclerAdapter<SchoolCarModel> recyclerAdapter;
    private Toolbar toolbar;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_school_bus);
        initView();
        Intent intent = getIntent();
        String json_totalList = intent.getStringExtra("totalList");
        totalList = GsonUtil.fromJson(json_totalList, SchoolCarModel.class);
        districtList = (List<String>) intent.getSerializableExtra("districtList");
        startPointList = (List<List<String>>) intent.getSerializableExtra("startPointList");
        Logger.d(startPointList.toString());
        adapter_district = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, districtList);
        adapter_startPoint = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, startPointStringList);
        adapter_district.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        updateStartPointSpinner(0);
        //绑定 Adapter到控件
        spinnerDistrict.setAdapter(adapter_district);
        spinnerStartPoint.setAdapter(adapter_startPoint);
        spinnerDistrict.setDropDownVerticalOffset(30);
        spinnerStartPoint.setDropDownVerticalOffset(30);
        selectedDistrict=districtList.get(0);
        selectedStartPoint=startPointStringList.get(0);
        spinnerDistrict.setSelection(0);
        spinnerDistrict.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                updateStartPointSpinner(pos);
                selectedDistrict=districtList.get(pos);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        spinnerStartPoint.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedStartPoint = startPointStringList.get(position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void updateStartPointSpinner(int pos) {
        startPointStringList.clear();
        for (String s : startPointList.get(pos)) {
            startPointStringList.add(s);
        }
        spinnerStartPoint.setSelection(0);
        selectedStartPoint=startPointStringList.get(0);
        adapter_startPoint.notifyDataSetChanged();
    }

    public static void startSearchSchoolBusActivity(Context context, List totalList, List districtList, List startPointList) {
        if (!(context instanceof SchoolBusActivity))
            return;
        Intent intent = new Intent(context, SearchSchoolBusActivity.class);
        String json_totalList = new Gson().toJson(totalList);
        intent.putExtra("totalList", json_totalList);
        intent.putExtra("districtList", (ArrayList) districtList);
        intent.putExtra("startPointList", (ArrayList) startPointList);
        context.startActivity(intent);
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        spinnerDistrict = (Spinner) findViewById(R.id.spinner_district);
        spinnerStartPoint = (Spinner) findViewById(R.id.spinner_startPoint);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        btnSearch = (Button) findViewById(R.id.btn_search);
        setSupportActionBar(toolbar);
        toolbar.setTitle("校车查询");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        recyclerAdapter = new BaseRecyclerAdapter<SchoolCarModel>(this, displayList) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.recycler_item_school_bus;
            }
            @Override
            public void bindData(RecyclerViewHolder holder, int position, SchoolCarModel item) {
                holder.setText(R.id.tv_busId, String.valueOf(item.getBid()))
                        .setText(R.id.tv_district, item.getBdistrict())
                        .setText(R.id.tv_start, item.getBstart())
                        .setText(R.id.tv_time, item.getBtime())
                        .setText(R.id.tv_route, item.getBroute());
            }
        };
        recyclerView.setAdapter(recyclerAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(selectedDistrict)||TextUtils.isEmpty(selectedStartPoint))
                    return;
                Logger.d(selectedStartPoint);
                Logger.d(selectedDistrict);
                displayList.clear();
                for (SchoolCarModel schoolCarModel : totalList) {
                    if (schoolCarModel.getBdistrict().equals(selectedDistrict) &&
                            schoolCarModel.getBstart().equals(selectedStartPoint)) {
                        displayList.add(schoolCarModel);
                    }
                }
                if (displayList.isEmpty())
                    Toast.makeText(SearchSchoolBusActivity.this, "亲~并符合条件的校车哦~", Toast.LENGTH_SHORT).show();
                recyclerAdapter.notifyDataSetChanged();
            }
        });
    }
}
